﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 金币类
public class CoinAI : MonoBehaviour {
	private Vector2 moveVector;
	private int moveingID = 0;

	public int coinColor;
	private Vector3 endPos;
	private float dropTime;
	private float speedTime;
	private float deltaHeight;
	// 在地上时间
	private float hideContinueTime;
	// 状态
	private int state;
	private int STATE_NONE = 0;
	private int STATE_DROP = 1;
	private int STATE_NORMAL = 2;
	private int STATE_HOLD = 3;
	private int STATE_UNDER = 4;

	// Use this for initialization
	void Start () {
		// randomMove ();
		// InvokeRepeating ("randomMove" , 3 , weakTime);
	}

	// addWidth addHeight 从多高的地方落下
	public void initCoin (int colorNum, float time, int addWidth, int addHeight) 
	{
		var colorRender = this.GetComponent<SpriteRenderer> ();
		coinColor = colorNum;
		
		
		CircleCollider2D collider = this.GetComponent<CircleCollider2D> ();
		// collider.isTrigger = false;
		collider.enabled = false;
		
		endPos = transform.position;
		transform.position = new Vector3(transform.position.x + addWidth, transform.position.y + addHeight, transform.position.z + 40);
		dropTime = time;
		deltaHeight = addHeight;
		state = STATE_DROP;
		hideContinueTime = 0.0f;

		StopCoroutine ("finishDroping");
		StartCoroutine ("finishDroping", time); 
	}

	// 结束掉落动作
	IEnumerator finishDroping (float time) 
	{
		yield return new WaitForSeconds (time);
		CircleCollider2D collider = this.GetComponent<CircleCollider2D> ();
		// collider.isTrigger = true;
		collider.enabled = true;
		state = STATE_NORMAL;
		transform.position = endPos;
	}

	
	// 每帧位移实现掉落效果
	void FixedUpdate () {
		// 超过时间自动隐藏
		if(state == STATE_NORMAL)
		{
			hideContinueTime += Time.deltaTime;
			float curOpacity = 1.0f - hideContinueTime / Const.coinAutoHideTime;
			if (curOpacity < Const.coinMinOpacity)
				curOpacity = Const.coinMinOpacity;
			if (curOpacity > 1)
				curOpacity = 1.0f;
			setCurOpacity(curOpacity);
			if (hideContinueTime >= Const.coinAutoHideTime)
			{
				autoHiden();
			}
		}
		else
		{
			hideContinueTime = 0.0f;
			float curOpacity = 1.0f;
			if(state == STATE_UNDER)
			{
				curOpacity = Const.coinMinOpacity;
			}
			setCurOpacity(curOpacity);
		}
		if(state != STATE_DROP)
			return;
		speedTime = deltaHeight / dropTime * Time.deltaTime;
		transform.position = new Vector3(Mathf.MoveTowards(transform.position.x, endPos.x, speedTime), Mathf.MoveTowards(transform.position.y, endPos.y, speedTime), Mathf.MoveTowards(transform.position.z, endPos.z, speedTime));	
	}
	// 几个处理状态函数
	public bool canHoldCoin()
	{
		return state == STATE_NORMAL;
	}

	public void onHoldingCoin()
	{
		state = STATE_HOLD;
		// setCurOpacity(1.0f);
	}

	public void onDropingCoin()
	{
		state = STATE_NORMAL;
	}

	public bool canDigOut()
	{
		return state == STATE_UNDER;
	}

	public void autoHiden()
	{
		state = STATE_UNDER;
		// setCurOpacity(0.5f);
	}

	public void onDiggingOut()
	{
		state = STATE_NORMAL;
		// setCurOpacity(1.0f);
	}

	public void setCurOpacity(float curNum)
	{
		var colorRender = this.GetComponent<SpriteRenderer> ();
		colorRender.color = new Vector4(colorRender.color.r, colorRender.color.g, colorRender.color.b, curNum);
	}
	

	// void randomMove(int[] randoms){
	// 	moveingID = Random.Range (0 , randoms.Length);
	// 	if (randoms[moveingID] == 0) {
	// 		moveVector = Vector2.up;
	// 	} else if (randoms[moveingID] == 1) {
	// 		moveVector = Vector2.down;
	// 	} else if (randoms[moveingID] == 2) {
	// 		moveVector = Vector2.left;
	// 	} else {
	// 		moveVector = Vector2.right;
	// 	}
	// }

	// void randomMove(){
	// 	moveingID = Random.Range (0 , 4);
	// 	if (moveingID == 0) {
	// 		moveVector = Vector2.up;
	// 	} else if (moveingID == 1) {
	// 		moveVector = Vector2.down;
	// 	} else if (moveingID == 2) {
	// 		moveVector = Vector2.left;
	// 	} else {
	// 		moveVector = Vector2.right;
	// 	}
	// }

    void OnTriggerEnter2D(Collider2D other)
    {
        
    }
}